package com.user.service;

import com.microservice.entity.User;
import com.user.feignClient.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.user.dao.UserMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService{

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserFeignClient userFeignClient;


	public User selectByPrimaryKey(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}


	public int login(String name,String loginPass) {
		User user = userMapper.getUserByName(name);
		if (user != null) {

			String pass = user.getPass();
			if (!loginPass.equals(pass)) {
				return -2;
			} else {
				return 1;
			}
		} else {
			return -1;
		}
	}


	/**
	 * 测试事务
	 *
	 * @return
	 */
	@Transactional
	public int saveUser(){
		User user = new User(2,"testuser","123456",22,"高新区","15042470205");
		userMapper.insert(user);

		//调用role的save方法，测试一个事务
		userFeignClient.saveRole();
		return 1;
	}

}
